package jcu.ict.profaid;

import java.util.HashMap;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.Html;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("NewApi")
public class MainActivity extends Activity {
	@SuppressWarnings("unused")
	private AlertDialogManager alert = new AlertDialogManager();
	private SessionManager session;
	private String id;
	
	@SuppressLint("NewApi")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		SignDbAdapter sdb = new SignDbAdapter(getBaseContext());

		session = new SessionManager(getApplicationContext());
		session.checkLogin();
		
		HashMap<String, String> user = session.getUserDetails();
		 
	    String email = user.get(SessionManager.KEY_EMAIL);
	    
	    sdb.open();
	    Cursor aCursor = sdb.populateUserID(email);
	    aCursor.moveToFirst();
	    
	    id = aCursor.getString(0);
	    
	    sdb.close();
	    
		ActionBar actionBar = getActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#0099CC"));
		actionBar.setBackgroundDrawable(colorDrawable);
		
		Button btnCourse = (Button) findViewById(R.id.btnCourse);
		btnCourse.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent myIntent = new Intent(MainActivity.this,
						MainCourse.class);
				Bundle bundle = new Bundle();
				bundle.putCharSequence("setUserId", id);
				myIntent.putExtras(bundle);
				startActivity(myIntent);
			}
		});
		
		Button btnHelp = (Button)findViewById(R.id.btnHelp);
		btnHelp.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent myIntent = new Intent(MainActivity.this,
						ViewFlipperMainActivity.class);
				startActivity(myIntent);
			}
		});
		
		
		TextView txtLogOut = (TextView)findViewById(R.id.txtLogout);
		txtLogOut.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				session.logoutUser();
				finish();
			}
		});
	}

	public boolean onKeyUp(int keyCode, KeyEvent event) 
	{
		if (keyCode == KeyEvent.KEYCODE_BACK)
		{
			System.exit(1);
		}
		return true;
	}

	
}
